package com.elecbook.content.service;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elecbook.base.dto.PageDTO;
import com.elecbook.base.enums.OperationType;
import com.elecbook.base.exception.BaseInfoException;
import com.elecbook.base.util.TimeFillUtil;
import com.elecbook.content.mapper.CommentMapper;
import com.elecbook.content.mapper.CommentjudgeMapper;
import com.elecbook.content.model.dto.CommentJudgeSubmitDTO;
import com.elecbook.content.model.dto.CommentjudgePQuery;
import com.elecbook.content.model.dto.CommnetJudgePageDTO;
import com.elecbook.content.model.po.Comment;
import com.elecbook.content.model.po.Commentjudge;
import com.elecbook.content.model.po.Contribute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author Z-ken
 * @since 2024/11/05 15:50:50
 */
@Service
public class CommentjudgeServiceImpl extends ServiceImpl<CommentjudgeMapper, Commentjudge> implements CommentjudgeService {

    @Autowired
    CommentjudgeMapper commentjudgeMapper;
    @Autowired
    CommentMapper commentMapper;

    @Override
    public Commentjudge submitJudge(CommentJudgeSubmitDTO dto) {

        if (dto.getCommentJudgeId() == null) {
            throw new BaseInfoException("未提交评论的id");
        }
        // 更新comment的状态
        Commentjudge commentjudge = commentjudgeMapper.selectById(dto.getCommentJudgeId());
        Comment comment = commentMapper.selectById(commentjudge.getCommentId());
        Integer judgeState = dto.getJudgeResult();
        commentjudge.setJudgeState(judgeState);
        comment.setJudgeState(judgeState);
        // 更新update时间
        TimeFillUtil.fillTimeAndCreateUser(comment, OperationType.UPDATE);

        // 进行更新状态
        commentjudgeMapper.updateById(commentjudge);
        commentMapper.updateById(comment);
        return commentjudge;
    }

    @Override
    public PageDTO<Commentjudge> queryCommentJudgePage(CommnetJudgePageDTO pageDTO) {
        // 1.添加排序规则
        // 使用内置的函数转换并且 设置规则
        List<OrderItem> orderItems = pageDTO.toMpOrderItems();

        // 获取分页对象和排序情况
        Page<Commentjudge> page = pageDTO.toMpPageDefaultSortByIdAsc(orderItems.toArray(new OrderItem[0]));
        /*
        查询, 自定义的分页查询
         */
        Page<Commentjudge> p = baseMapper.selectPageVO(page, pageDTO);

        // 获取数据信息
        return PageDTO.of(p, Commentjudge.class);
    }


    /**
     * 基础分页请求--管理员
     */
    @Override
    public PageDTO<Commentjudge> pageQuery(CommentjudgePQuery query) {
        // 1.添加排序规则
        // 使用内置的函数转换并且 设置规则-这里为了方便扩展和使用先进行创建
        List<OrderItem> orderItems = query.toMpOrderItems();

        // 获取分页对象和排序情况
        Page<Commentjudge> page = query.toMpPageDefaultSortByIdAsc(orderItems.toArray(new OrderItem[0]));
    /*
    查询
    */
        Page<Commentjudge> p = baseMapper.selectPageEntity(page, query);

        // 获取数据信息
        return PageDTO.of(p, Commentjudge.class);
    }


    @Override
    public Commentjudge saveCommentjudge(Commentjudge commentjudge) {
        commentjudge.setId(null);
        baseMapper.insert(commentjudge);
        return commentjudge;
    }

    @Override
    public Commentjudge updateCommentjudgeById(Commentjudge commentjudge) {
        baseMapper.updateById(commentjudge);
        return commentjudge;
    }
}
